var port;

       function save() {
         var delim = document.getElementById("txt_delimiter").value;
         var sort = document.getElementById("ddl_sort").value;
         var minutes = document.getElementById("txt_minutes").value;
         var folder = document.getElementById("ddl_folder").value;
	 var show = document.getElementById("show").checked;

	port.postMessage({"message": "saveoptions",
			  "delimiter": delim,
			  "sortorder": sort,
			  "checkfrequency": minutes,
			  "folder": folder,
			  "show": show
			});

	window.close();
       }
    
       function load(data) {
         var delim = document.getElementById("txt_delimiter");
         var sort = document.getElementById("ddl_sort");
         var minutes = document.getElementById("txt_minutes");
         var defaultfolder = document.getElementById("ddl_folder");
	 var show = document.getElementById("show");
         delim.value = data.delimiter;
         sort.value = data.sortorder;
         minutes.value = data.checkfrequency;
         defaultfolder.value = data.folder;
	 if (data.show == "true")
	  show.checked = data.show;

         update();
       }
       
       function update() {
         document.getElementById("minutes").innerHTML = document.getElementById("txt_minutes").value;
         document.getElementById("delimexample").innerHTML = document.getElementById("txt_delimiter").value;
         
         document.getElementById("warning").innerHTML = "Warning: this option will replace all your existing bookmarks with your Google Bookmarks.  To keep your existing bookmarks, choose 'Google Bookmarks' folder.<br/>";
         
         if (document.getElementById("ddl_folder").value == "Bookmarks bar") {
             document.getElementById("folderexample").innerHTML = "<b>Bookmarks bar</b> except where labeled <b>Other bookmarks</b>";
         } else if (document.getElementById("ddl_folder").value == "Google Bookmarks") {
             document.getElementById("folderexample").innerHTML = "<b>Google Bookmarks</b> on the bookmarks bar";
             document.getElementById("warning").innerHTML = "";
         } else {
             document.getElementById("folderexample").innerHTML = "<b>Other bookmarks</b> except where labeled <b>Bookmarks bar</b>";
         }
         
         if (document.getElementById("ddl_sort").value == "A") {
             document.getElementById("sortexample").innerHTML = "name";
         } else if (document.getElementById("ddl_sort").value == "D") {
             document.getElementById("sortexample").innerHTML = "date created";
         } else if (document.getElementById("ddl_sort").value == "U") {
             document.getElementById("sortexample").innerHTML = "you";
         }
       }

function init() {
  port = chrome.extension.connect({});
  port.onMessage.addListener(portOnMessage);
  port.postMessage({"message": "getoptions"});
  
  document.getElementById("txt_delimiter").onchange = update;
  document.getElementById("ddl_sort").onchange = update;
  document.getElementById("txt_minutes").onchange = update;
  document.getElementById("ddl_folder").onchange = update;
  document.getElementById("save").onclick = save;
}

function portOnMessage(data) {
  if (data.message == "options") {
    load(data);
    update();
  }
}

document.addEventListener("DOMContentLoaded", function() {
  init();
});